//
//  HELMeHeader.m
//  HandheldELearn
//
//  Created by wsj2015 on 2017/1/6.
//  Copyright © 2017年 陈武琦. All rights reserved.
//

#import "HELMeHeader.h"

@implementation HELMeHeader


+ (instancetype)header{
  
    HELMeHeader *header = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
    
    
    header.bgimg.image = [header originImage:header.bgimg.image scaleToSize:CGSizeMake(kScreenWidth, 246)];
   
    CGFloat b = 10;
    
    if (kScreenWidth == 320 && kScreenHeight ==480) {//iphone4
        
    }else if(kScreenWidth == 320 && kScreenHeight ==568){ //iphone 5
        
    }else if(kScreenWidth == 375 && kScreenHeight == 667){ //iphone 6
        b = 18;
    }else if(kScreenWidth == 414 && kScreenHeight == 736){ //iphone 6P
        b = 30;
    }
    
    header.avarBottom.constant = b;
    wsj_cornerRadius(header.iconBtn, 45);
    NSTimeInterval period = 3.5; //设置时间间隔
    dispatch_queue_t queue = dispatch_get_main_queue();
    header.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(header.timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(header.timer, ^{
        //在这里执行事件
        CGRect frame = header.bgimg.frame;
        
        
        if (frame.origin.x<0) {
            frame.origin.x =0 ;
        }else{
            frame.origin.x =-20;
        }
       
        
        [UIView animateWithDuration:3.5 animations:^{
            header.bgimg.frame =frame;
        }];
    });
    dispatch_resume(header.timer);
    return header;
    
}
- (void)scrollerChangeImageWithoffsetY:(CGFloat)offsetY {
    if (offsetY < 0) {
        if (offsetY < 0) {
            CGRect iconF = self.bgimg.frame;
            iconF.origin.y = offsetY;
            iconF.size.height = -offsetY+246;
            self.bgimg.frame = iconF;
        }
        
    }
    
}
#pragma mark - 给图片设置尺寸
- (UIImage *)originImage:(UIImage *)image scaleToSize:(CGSize)size{
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    UIImage * scaleImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaleImage;
}
- (void)updateBgimgFrame{
    
    CGRect frame = self.bgimg.frame;

     static int direction = 1;
    if (direction == 1) {
         frame.origin.x +=1;
        if (frame.origin.x>=0) {
            direction = -1;
        }
    }else {
         frame.origin.x -=1;
        if (frame.origin.x<=-20) {
            direction = 1;
        }
    }
    
   
    [UIView animateWithDuration:.2 animations:^{
        self.bgimg.frame =frame;
    }];
  
}
                           
                           
- (IBAction)personInfoAction:(id)sender {
    self.personInfoBlock();
    
}
- (IBAction)logAction:(id)sender {
    self.logBlock();
}
- (IBAction)learnRecordAction:(id)sender {
    self.scoreRecordBlock();
}
- (IBAction)changeHead:(id)sender {
    self.changeHeader();
}




@end
